# coding: utf-8

from peewee import IntegerField, Model, Proxy, SqliteDatabase, TextField

import config

from initialize import logger

database_proxy = Proxy()

class BaseModel(Model):
    class Meta:
        database = database_proxy

class PolicyInfo(BaseModel):
    class Meta:
        db_table = 'policy_info'
    id = IntegerField(primary_key=True)
    policy_no = TextField() # 保单号
    policy_name = TextField() # 投保人姓名
    policy_date_time = TextField() # 保险期间
    policy_begin_date_time = TextField() # 保险责任开始时间
    policy_status = TextField() # 保单状态
    policy_holder_name = TextField() # 投保人姓名
    policy_the_name = TextField() # 被保险人姓名
    policy_the_no = TextField() # 被保险人证件号码
    license_plate_no = TextField() # 车牌号
    frame_number = TextField() # 车架号
    engine_number = TextField() # 发动机号
    nuclear_load_number = TextField() # 核定载客数量
    nuclear_load_weight = TextField() # 核定载重质量
    vehicle_type = TextField() #车辆类型
    vehicle_usage = TextField() #车辆使用性质
    pay_premium = TextField() # 应缴保费
    paid_premium = TextField() # 已缴保费
    pay_method = TextField()  # 缴费方式
    pay_date = TextField() # 缴费日期
    unpaid_premium = TextField() # 欠缴保费
    channel_type = TextField() # 渠道类型
    agency_name = TextField() # 代理机构/人名称
    agency_code = TextField() # 机构代码/职业资格证书代码
    payment_method = TextField() # 支付方式
    payment_object = TextField() # 支付对象

    # def mapper(self, entity):
    #     self.id = entity.id
    #     self.policy_no = entity.policy_no
    #     self.policy_name = entity.policy_name
    #     self.policy_date_time = entity.policy_date_time
    #     self.policy_begin_date_time = entity.policy_begin_date_time
    #     self.policy_status = entity.policy_status
    #     self.policy_holder_name = entity.policy_holder_name
    #     self.policy_the_name = entity.policy_the_name
    #     self.policy_the_no = entity.policy_the_no
    #     self.license_plate_no = entity.license_plate_no
    #     self.frame_number = entity.frame_number
    #     self.engine_number = entity.engine_number
    #     self.nuclear_load_number = entity.nuclear_load_number
    #     self.nuclear_load_weight = entity.nuclear_load_weight
    #     self.vehicle_type = entity.vehicle_type
    #     self.vehicle_usage = entity.vehicle_usage
    #     self.pay_premium = entity.pay_premium
    #     self.paid_premium = entity.paid_premium
    #     self.pay_method = entity.pay_method
    #     self.pay_date = entity.pay_date
    #     self.unpaid_premium = entity.unpaid_premium
    #     self.channel_type = entity.channel_type
    #     self.agency_name = entity.agency_name
    #     self.agency_code = entity.agency_code
    #     self.payment_method = entity.payment_method
    #     self.payment_object = entity.payment_object
    #     self.insurance_type_list = entity.insurance_type_list


class InsuranceType(BaseModel):
    class Meta:
        db_table = 'insurance_type'
    id = IntegerField(primary_key=True)
    policy_no = TextField()  # 保单号
    guarantee_project = TextField() # 保障项目
    sum_insured = TextField() # 保额
    annual_premium = TextField() # 标准保费
    rate_floating_coefficient = TextField() # 费率浮动系数
    payment_premium = TextField() # 实缴保费
    rate_floating_cause = TextField() # 费率浮动事由

class PolicyRecord(BaseModel):
    class Meta:
        db_table = 'policy_record'
    id = IntegerField(primary_key=True)
    policy_no = TextField()
    status = IntegerField()

class DatabaseHandler(object):

    def __init__(self):
        logger.info('initialize database!')
        self.database = SqliteDatabase(config.DATABASE_FILE)
        database_proxy.initialize(self.database)
        self.connect()
        tables = [PolicyRecord, PolicyInfo, InsuranceType]
        for t in tables:
            if t.table_exists() is False:
                t.create_table()
        self.close()
        logger.info()

    def get_db(self):
        return self.database

    def connect(self):
        self.database.connect()

    def commit(self):
        self.database.commit()

    def close(self):
        self.database.close()

    def get_query_record(self, policy_no):
        policy_record_list = PolicyRecord.select().where(PolicyRecord.policy_no==policy_no)
        if policy_record_list:
            return policy_record_list[0]
        else:
            return None

    def insert(self, object):
        self.connect()
        object.save()
        self.commit()
        self.close()

    def insert_policy(self, policy_info_entity):
        policy_info = policy_info_entity.mapper(PolicyInfo)
        self.connect()
        policy_record = PolicyRecord()
        policy_record.policy_no = policy_info.policy_no
        policy_record.status = 1
        policy_record.save()
        policy_info.save()
        insurance_type_list = policy_info_entity.insurance_type_list
        for insurance_type in insurance_type_list:
            insurance_type.policy_no = policy_info.policy_no
            insurance_type.save()
        self.commit()
        self.close()
